# 导包
import unittest
import requests
from api.login import LoginApi
from parameterized import parameterized
from tools.dbutil import DButil


def build_data():
    sql = "select * from t_login"
    db_data = DButil.exe_sql(sql)
    test_data = []
    for case_data in db_data:
        username = case_data[2]
        password = case_data[3]
        verify_code = case_data[4]
        status_code = case_data[5]
        status = case_data[7]
        msg = case_data[8]
        test_data.append((username,password,verify_code,status_code,status,msg))
    return test_data

#定义测试类
class TestLoginApil(unittest.TestCase):
    #前置处理
    def setUp(self):
        self.login_api = LoginApi()
        self.session = requests.Session()

    #后置处理
    def tearDown(self):
        self.session.close()

    # 定义测试方法
    # 登录成功
    @parameterized.expand(build_data)
    def test01_login(self,username,password,verify_code,status_code,status,msg):
        # 调用验证码接口并且断言
        respones = self.login_api.get_verity_code(self.session)
        self.assertEqual(200,respones.status_code)
        self.assertIn("image", respones.headers.get("Content-Type"))
        # 调用登录接口并且断言
        response = self.login_api.login(self.session, username, password, verify_code)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))
